package com.knife.blog.web.action;

import com.knife.blog.domain.BlogCircle;
import com.knife.blog.domain.BlogCircleMember;
import com.knife.blog.domain.BlogUser;
import com.knife.blog.logic.BlogCircleMemberService;
import com.knife.blog.logic.impl.BlogCircleMemberServiceImpl;
import com.knife.util.CommUtil;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;

public class BlogCircleMemberAction extends BaseCrudAction {
	
	private BlogCircleMemberService circleMemberService = BlogCircleMemberServiceImpl.getInstance();

	public Object doBefore(WebForm form, Module module) {
		if(this.getCurrentUser(form)!=null){
			blog = userService.getUserBlog(this.getCurrentUser(form).getUserName());
		}
		return super.doBefore(form, module);
	}
	
	@Override
	public IPageList doQuery(WebForm form, int currentPage, int pageSize) {
		// TODO 自动生成方法存根
		String cid = (String)form.get("cid");
		return circleMemberService.queryCircleMemberByCircleCid(cid,currentPage, pageSize);
	}
	
	public Page doJoin(WebForm form, Module module){
		BlogCircleMember member = circleMemberService.getCircleMember((String)form.get("cid"));
		member.setStatus(new Integer(1));
		boolean ret = circleMemberService.update(member);
		form.addResult("msg", ret ? member.getInputuser()+"成功加入圈子!" : "审核失败!");
		return super.doQuery(form, module, this.getCurrentUser(form));
	}
	
	public Page doDel(WebForm form, Module module){
		String cid = (String)form.get("cid");
		boolean ret = circleMemberService.delCircleMember(cid);
		form.addResult("msg", ret?"删除成功！":"删除失败！");
		return super.doQuery(form, module, this.getCurrentUser(form));
	}
	
	public Page doShowApply(WebForm form, Module module){
		IActiveUser user=getCurrentUser(form);
		if(user == null){
			form.addResult("msg", "请先登陆！");
			return module.findPage("login");
		}
		form.addResult("cid", (String)form.get("cid"));
		return module.findPage("apply");
	}
	
	public Page doApply(WebForm form, Module module){
		String username = (String)form.get("username");
		BlogUser user = userService.getBlogUser(username);
		if(user==null){
			form.addResult("msg", "用户不存在！请确认用户名填写无误！");
			return module.findPage("apply");
		}
		BlogCircleMember member = circleMemberService.getCircleMember(username);
		if(member!=null){
			form.addResult("msg", "该成员已经存在，可能你已经是该圈子成员！");
		}
		member = circleMemberService.blogUser2BlogCircleMember(user);
		member.setNickname((String)form.get("nickname"));
		member.setSn((String)form.get("cid"));
		member.setStatus(0);
		boolean ret = circleMemberService.save(member);
		form.addResult("msg", ret?"申请已提交，请等待管理员审核！":"提交失败！");
		return module.findPage("apply");
	}
	
	public Page doMemberManage(WebForm form,Module module){
		Page forward = null;
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		String cid = (String)form.get("cid");
		IPageList pList =  circleMemberService.queryCircleMemberByCircleCid(cid,currentPage, pageSize);
		if (pList != null) {
			form.addResult("list", pList.getResult());
			form.addResult("pages", new Integer(pList.getPages()));
			form.addResult("rows", new Integer(pList.getRowCount()));
			form.addResult("page", new Integer(pList.getCurrentPage()));
			form.addResult("gotoPageHTML", CommUtil.showPageHtml(pList
					.getCurrentPage(), pList.getPages()));
		}
		return crudPage(form,module,"list");
	}

	@Override
	public Object form2Obj(WebForm form) {
		// TODO 自动生成方法存根
		String cid = CommUtil.null2String(form.get("cid"));
		BlogCircleMember obj = (BlogCircleMember)circleMemberService.getCircleMember(cid);
		if (obj == null)
			obj = new BlogCircleMember();
		form.toPo(obj);
		return obj;
	}

}
